<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="onChangeReset">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="申请时间" class="w-[320px] mr-2">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" class="w-[260px]">
          <a-input v-model:value="formState.zjm" placeholder="申请单号/病人姓名/助记码" />
        </a-form-item>
        <a-form-item class="w-[280px] ml-2">
          <a-radio-group v-model:value="formState.appPath">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">门诊</a-radio>
            <a-radio value="1">住院</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="000641" :scroll="{ x: 2400 }" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'appPath'">
            <span v-if="record?.appPath === 1" color="blue">住院</span>
            <span v-else color="red">门诊</span>
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record?.status === 4" color="blue">已发血</a-tag>
            <a-tag v-else color="red">未发血</a-tag>
          </template>
          <template v-if="column.dataIndex === 'admissionCode'">
            {{ record.patientInfo?.inpRecordCode }}
          </template>
          <template v-if="column.dataIndex === 'patientInfo'">
            <template v-if="record?.patientInfo">
              <span>{{ record.patientInfo?.brName }}</span>
              <span class="mx-2">{{ record.patientInfo?.brXb }}</span>
              <span>{{ record.patientInfo?.brAge }}</span>
              <span v-if="record?.appPath === 1" class="mx-2">{{ record.patientInfo?.bedCode }}</span>
              <span>{{ record.patientInfo?.deptName }}</span>
              <span v-if="record?.appPath === 1" class="mx-2">{{ record.patientInfo?.wardName }}</span>
            </template>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" :disabled="record?.status === 4" @click="onClickConfirm(record)"> 发血 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  appPath: ''
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/BldTransfusionRecord/getBldTransfusionDistributeList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

// 发血
const onClickConfirm = async (row) => {
  const { code, msg } = await post('/bloodm/BldTransfusionRecord/doUpdateBldTransfusionApply', { appLsh: row.appLsh, status: 4 })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

const onChangeReset = () => {
  formState.value = {
    dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
    appPath: ''
  }
  getData()
}
</script>
